﻿using System;
/*
 * We are given integer number n, value v (v=0 or 1) and a position p. 
 * Write a sequence of operators that modifies n to hold the value v 
 * at the position p from the binary representation of n.
 * Example: n = 5 (00000101), p=3, v=1  13 (00001101)
 * n = 5 (00000101), p=2, v=0  1 (00000001)
 */

class SetACertainBitValue
{
    static void Main(string[] args)
    {
        int number = 5;
        int value = 1;
        int position = 3;

        String numberBinary = Convert.ToString(number, 2).PadLeft(32, '0');
        Console.WriteLine("Set the bit at position {0} of the binary representation of {1} to {2} ", position, number, value);
        Console.WriteLine("Before the change the number is {0}: {1}", number, numberBinary);
        int mask;
        int result;
        if (value == 1)
        {
            mask = 1 << position;
            result = number | mask;
        }
        else
        {
            mask = ~(1 << position);
            result = number & mask;
        }
        string resultBinary = Convert.ToString(result, 2).PadLeft(32, '0');
        Console.WriteLine("After the change the number is {0}: {1}", result, resultBinary);    
    }
}
